package com.scrm.server.wx.cp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scrm.api.wx.cp.entity.WxCustomer;
import com.scrm.api.wx.cp.entity.WxFissionContact;
import com.scrm.api.wx.cp.entity.WxFissionCustomer;
import com.scrm.api.wx.cp.entity.WxFissionTask;
import com.scrm.api.wx.cp.vo.FissionInviteConditionVO;
import com.scrm.api.wx.cp.vo.WxFissionCustomerVO;
import com.scrm.common.exception.BaseException;
import com.scrm.common.util.UUID;
import com.scrm.server.wx.cp.mapper.WxFissionCustomerMapper;
import com.scrm.server.wx.cp.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 企微应用宝-客户信息 服务实现类
 * @author xxh
 * @since 2022-03-21
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class WxFissionCustomerServiceImpl extends ServiceImpl<WxFissionCustomerMapper, WxFissionCustomer> implements IWxFissionCustomerService {

    @Autowired
    private IWxCustomerService customerService;

    @Autowired
    private IWxFissionContactService fissionContactService;

    @Autowired
    private IWxCustomerLossInfoService lossInfoService;

    @Autowired
    private IWxFissionTaskCustomerDetailService customerDetailService;

    @Autowired
    private IWxFissionTaskService taskService;

    /**
     * 翻译
     * @param wxFissionCustomer 实体
     * @return WxFissionCustomerVO 结果集
     * @author xxh
     * @date 2022-03-21
     */
    private WxFissionCustomerVO translation(WxFissionCustomer wxFissionCustomer, boolean hasCheckDelete){
        WxFissionCustomerVO vo = new WxFissionCustomerVO();
        BeanUtils.copyProperties(wxFissionCustomer, vo);

        WxCustomer customer = customerService.find(wxFissionCustomer.getExtCorpId(), wxFissionCustomer.getExtCustomerId());

        //状态
        if (!wxFissionCustomer.getHasFirst()) {
            vo.setStatus(WxFissionCustomerVO.NO_FIRST);
        }else if(hasCheckDelete
                && wxFissionCustomer.getHasDeleted() != null
                && wxFissionCustomer.getHasDeleted()){
            vo.setStatus(WxFissionCustomerVO.IS_DELETE);
        }else{
            vo.setStatus(WxFissionCustomerVO.SUCCESS);
        }
        return vo.setCustomer(customer);
    }


    @Override
    public WxFissionCustomer checkExists(String id){
        if (StringUtils.isBlank(id)) {
            return null;
        }
        WxFissionCustomer byId = getById(id);
        if (byId == null) {
            throw new BaseException("企微应用宝-客户信息不存在");
        }
        return byId;
    }

    @Override
    public IPage<WxFissionCustomerVO> getInviteDetails(FissionInviteConditionVO dto) {

        LambdaQueryWrapper<WxFissionCustomer> wrapper = new QueryWrapper<WxFissionCustomer>().lambda()
                .eq(WxFissionCustomer::getExtCorpId, dto.getExtCorpId())
                .eq(WxFissionCustomer::getTaskId, dto.getTaskId())
                .eq(WxFissionCustomer::getExtInviteCustomerId, dto.getExtCustomerId())
                .orderByDesc(WxFissionCustomer::getCreatedAt);
        IPage<WxFissionCustomer> page = page(new Page<>(dto.getPageNum(), dto.getPageSize()),wrapper);

        //查出任务
        WxFissionTask task = taskService.checkExists(dto.getTaskId());

        return page.convert(e -> translation(e, task.getHasCheckDelete()));
    }

    @Override
    public void updateCustomerLose(String extCorpId, String customerExtId, String extStaffId) {

        update(null, new UpdateWrapper<WxFissionCustomer>().lambda()
                .eq(WxFissionCustomer::getExtCorpId, extCorpId)
                .eq(WxFissionCustomer::getExtCustomerId, customerExtId)
                .eq(WxFissionCustomer::getExtStaffId, extStaffId)
                .set(WxFissionCustomer::getHasDeleted, true));

    }

    @Override
    public void refreshCustomer(String extCorpId, String extCustomerId, String extStaffId, Boolean isNewCustomer, WxFissionContact contact) {

        //新增
        WxFissionCustomer wxFissionCustomer = new WxFissionCustomer().setId(UUID.get32UUID())
                .setExtCorpId(extCorpId).setTaskId(contact.getTaskId())
                .setExtInviteCustomerId(contact.getExtCustomerId())
                .setExtCustomerId(extCustomerId).setExtStaffId(extStaffId)
                .setHasFirst(isNewCustomer).setHasDeleted(false)
                .setCreatedAt(new Date()).setUpdatedAt(new Date());

        save(wxFissionCustomer);

        //更新完成情况
        customerDetailService.updateFinishStatus(extCorpId, contact.getTaskId(), contact.getExtCustomerId());
    }
}
